package com.apache.rpc.dao;

import java.util.ArrayList;
import java.util.List;

import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.DaoImpl;
import com.apache.database.model.MethodParam;
import com.apache.rpc.entity.InterfaceRegister;

public class InterfaceRegisterDaoImpl extends DaoImpl {

	public String getSql(int type) {
		String sql = "";
		switch (type) {
		case 1:
			sql = "select count(*) from InterfaceRegister where 1=1 ";
			break;
		case 2:
			sql = " from InterfaceRegister where 1=1 ";
			break;
		default:
			sql = "select count(*) from InterfaceRegister where 1=1 ";
		}
		return sql;
	}

	public void initSystemCache() {
		MethodParam param = new MethodParam("ByProperty", "", getSql(2), "com.apache.rpc.entity.InterfaceRegister");
		List list = this.select(param);
		if (!Validator.isEmpty(list)) {
			int count = list.size();
			for (int i = 0; i < count; i++) {
				List<String> vos = null;
				InterfaceRegister register = (InterfaceRegister) list.get(i);
				String key = "interfaceRegister_" + register.getBeanId();
				if (Validator.isEmpty(SystemTools.getInstance().getCache("").getCacheObjectByKey(key))) {
					vos = new ArrayList<String>();
				} else {
					vos = (List) SystemTools.getInstance().getCache("").getCacheObjectByKey(key);
				}
				vos.add(register.getRegisterId());
				SystemTools.getInstance().getCache("").createCacheObject(key, vos);
				SystemTools.getInstance().getCache("")
						.createCacheObject("interfaceRegister_" + register.getRegisterId(), register);
			}
			SystemTools.getInstance().getCache("").createCacheObject("interfaceRegisterList", "true");
		}
	}
}
